package vip.mengqin.diandezhun.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.util.GlideUtil;
import com.android.baselibrary.view.MultiStateView;
import com.android.commcount.Lib_CommCount;
import com.android.commcount.bean.CommCount_Type;
import com.android.commcount.ui.activity.MyCountTypeListActivity;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.BindView;
import butterknife.OnClick;
import vip.mengqin.diandezhun.R;
import vip.mengqin.diandezhun.bean.UserInfo;
import vip.mengqin.diandezhun.manager.API_Manager;
import vip.mengqin.diandezhun.manager.OneKeyLoginManager;
import vip.mengqin.diandezhun.manager.UserManager;
import vip.mengqin.diandezhun.ui.activity.AboutUsActivity;
import vip.mengqin.diandezhun.ui.activity.Mine_MyInvoiceActivity;
import vip.mengqin.diandezhun.ui.activity.RechargeCenterActivity;
import vip.mengqin.diandezhun.ui.activity.SettingActivity;
import vip.mengqin.diandezhun.ui.activity.UserCenterActivity;

public class HomeMineFragment extends BaseFragment {

    @BindView(R.id.ll_head)
    LinearLayout ll_head;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_day)
    TextView tv_day;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_mine;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).titleBar(ll_head).statusBarDarkFont(true).navigationBarDarkIcon(true).init();

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mRootView != null) {
            showUserInfo();
            if (UserManager.isLogin(mContext)) {
                getUserInfo();
            }
        }
    }

    //刷新用户信息
    private void showUserInfo() {
        if (UserManager.isLogin(mContext)) {
            UserInfo userInfo = UserManager.getUserInfo(mContext);
            if (userInfo != null) {
                tv_name.setText(userInfo.nickname);
                GlideUtil.displayImage(mContext, userInfo.avatar, iv_head, R.drawable.ico_default_img_fang);
                //使用天数
                int day = (int) ((System.currentTimeMillis() / 1000 - userInfo.regTimestamp) / (24 * 60 * 60));
                tv_day.setText(day + "");

            }
        } else {
            //未登录

        }
    }

    //获取用户信息
    private void getUserInfo() {
        API_Manager.getUserInfo(mContext, new OkHttpCallBack<BaseResponce<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponce<UserInfo> baseResponce) {
                UserManager.saveUserInfo(mContext, baseResponce.getData());
                showUserInfo();
            }

            @Override
            public void onFailure(BaseResponce<UserInfo> baseResponce) {
            }
        });
    }

    @OnClick({R.id.ctv_guanyuwomen, R.id.ctv_setting, R.id.ctv_wodefapiao, R.id.ctv_goumaimoban, R.id.ctv_yigoumaimoban, R.id.ll_userinfo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ctv_guanyuwomen:  //关于我们
                StartActivity(AboutUsActivity.class);
                break;
            case R.id.ctv_setting:  //设置
                StartActivity(SettingActivity.class);
                break;
            case R.id.ctv_wodefapiao:   //我的发票
                StartActivity(Mine_MyInvoiceActivity.class);
                break;
            case R.id.ctv_goumaimoban:    //充值中心
                Lib_CommCount.getInstance().getType_Home(mContext, new CommCallBack() {
                    @Override
                    public void onResult(Object obj) {
                        CommCount_Type countType = (CommCount_Type) obj;
                        Intent intent = new Intent(mContext, RechargeCenterActivity.class);
                        intent.putExtra("templateId", countType.id);
                        startActivity(intent);
                    }
                });
                break;
            case R.id.ctv_yigoumaimoban:
                StartActivity(MyCountTypeListActivity.class);
                break;
            case R.id.ll_userinfo:  //个人中心
                StartActivity(UserCenterActivity.class);
                break;
        }
    }


}
